<template>
  <div class="app-container">

    <MmvTitle title="订单费用"></MmvTitle>
    <el-table :data="detail.chargingItems" border style="width: 100%; margin-top: 20px">
      <el-table-column _min-width="200" label="费用名称" align="center" prop="name" />
      <el-table-column _min-width="200" label="收费金额" align="center" prop="chargeMoney" />
      <el-table-column _min-width="160" label="支付方式" align="center" prop="methodPayment"/>
      <el-table-column _min-width="160" label="支付状态" align="center" prop="payStatusStr"/>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary"
            v-if="scope.row.payStatus==1&&scope.row.payType==2&&detail.orderStatus<7"
            @click="chargeRentOrDeposit(scope.row)">确认收款</el-button>
          <el-button size="mini" type="primary" v-if="scope.row.payStatus==2&&scope.row.payType==2"
            @click="viewVoucher(scope.row)">查看凭证</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <br />
    <div v-if="detail.orderStatus>4&&detail.orderStatus<10">
      <MmvTitle title="租车押金扣款明细"></MmvTitle>
      <el-table v-loading="loading" :data="detail.deductTheDeposit" border style="width: 100%; margin-top: 20px"
        show-summary :summary-method="getSummaries">
        <el-table-column min-width="200" label="扣款原因" align="center" prop="deductionsFor" />
        <el-table-column min-width="200" label="扣款金额" align="center" prop="deductionsAmount" />
<!--        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-search" @click="handleDetail(scope.row.id,1)">详情</el-button>
          </template>
        </el-table-column>-->
      </el-table>
    </div>
    <br />
    <div v-if="detail.orderRefunds.length>0">
      <MmvTitle title="退款费用明细">
      </MmvTitle>
      <el-table v-loading="loading" :data="detail.orderRefunds" :show-summary="true" border
        :summary-method="getSummaries">
        <el-table-column min-width="100" label="退款类型" align="center" prop="type">
          <template slot-scope="{row}">
            <dict-tag :options="dict.type.refund_type" :value="row.type" />
          </template>
        </el-table-column>
        <el-table-column min-width="100" label="退款金额" align="center" prop="refundAmount" />
        <el-table-column min-width="150" label="退款时间" align="center" prop="refundTime" />
        <el-table-column min-width="200" label="退款流水号" align="center" prop="serialNumber" />
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-if="!scope.row.serialNumber"
              @click="viewRefundVoucher(scope.row.type)">查看凭证</el-button>
          </template>
        </el-table-column>-->
      </el-table>
    </div>
    <br />
    <div v-if="detail.payTheCosts.length>0">
      <MmvTitle title="补缴费用明细">
        <!-- <div slot="btn">
        <el-button size="mini" type="primary" @click="confirmReceiptAll()" :loading="btnLoadIng" v-if="detail.payTheCostsNoPay>0">一键收款
        </el-button>
      </div> -->
      </MmvTitle>
      <el-table v-loading="loading" :data="detail.payTheCosts" :show-summary="true" border
        :summary-method="getSummaries">
        <el-table-column min-width="200" label="补缴原因" align="center" prop="remark" />
        <el-table-column min-width="200" label="补缴金额" align="center" prop="paidAmount" />
        <el-table-column min-width="160" label="支付状态" align="center" prop="payStatus">
          <template slot-scope="{row}">
            <dict-tag :options="dict.type.order_payStatus" :value="row.payStatus" />
          </template>
        </el-table-column>
<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-if="scope.row.payStatus==2"
              @click="viewCostsVoucher(scope.row.bizType)">查看凭证</el-button>
          </template>
        </el-table-column>-->
      </el-table>
    </div>
    <el-dialog title="支付凭证" :visible.sync="showpingzheng" width="500px" destroy-on-close append-to-body
      @close="closeDiolog">
      <PingZheng ref="PingZheng" v-if="showpingzheng" :bizId="orderNo" :bizType="bizType" :edit="pingzheng"
        @submit="doChargeRentOrDeposit"></PingZheng>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="showdatail" append-to-body width="800px" @close="nowId=null">
      <koukuanyuany v-if="showdatail" :id="nowId"></koukuanyuany>
    </el-dialog>

    <!-- 违章详情 -->
    <el-dialog title="违章详情" :visible.sync="showIllegalInfo" width="900px" destroy-on-close append-to-body
      @close="nowId=null">
      <MmvInfo ref="MmvInfo" :id="nowId" v-if="showIllegalInfo"></MmvInfo>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showIllegalInfo=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    costDetail,
    confirmReceipt,
    confirmReceiptAll,
    chargeDeposit,
    chargeRent
  } from '@/api/longOrder/index';
  import {
    depositRefund,
    IllegalRefund
  } from "@/api/order/settlement";
  import MmvTitle from '@/components/MmvUI/MmvTitle.vue';
  import PingZheng from '../../components/zhifupingzheng.vue';
  import koukuanyuany from './components/koukuanyuany.vue';
  import MmvInfo from '../../car/illegal/info.vue';
  export default {
    components: {
      MmvTitle,
      PingZheng,
      koukuanyuany,
      MmvInfo
    },
    dicts: ['sys_pay_type', 'order_payStatus', 'charge_type', 'refund_type'],
    props: ['orderNo'],
    data() {
      return {
        showdatail: false,
        showIllegalInfo: false,
        nowId: null,
        detail: {
          payTheCosts: [],
          orderRefunds: []
        },
        loading: true,
        pingzheng: true,
        showpingzheng: false,
        btnLoadIng: false,
        bizType: 0
      }
    },
    mounted() {
      this.costDetail();
    },
    methods: {
      closeDiolog() {
        this.pingzheng = true
        this.costDetail();
      },
      costDetail() {
        costDetail(this.orderNo).then(response => {
          this.detail = response.data || {};
          this.loading = false
        });
      },
      chargeRentOrDeposit(row) {
        this.bizType = row.type
        this.exportLoading = true
        this.showpingzheng = true
      },
      viewVoucher(row) {
        this.bizType = row.type
        this.showpingzheng = true
        this.pingzheng = false
      },
      viewCostsVoucher(bizType) {
        this.bizType = bizType == 3 ? 3 : 5
        this.showpingzheng = true
        this.pingzheng = false
      },
      viewRefundVoucher(bizType) {
        switch (bizType) {
          case 1:
            this.bizType = 8;
            break;
          case 2:
            this.bizType = 4;
            break;
          case 3:
            this.bizType = 6;
            break;
        }
        this.showpingzheng = true
        this.pingzheng = false
      },
      handleDetail(nowId, type) {
        this.nowId = nowId;
        if (type == 1) {
          this.showdatail = true;
        } else {
          this.showIllegalInfo = true
        }

      },
      doChargeRentOrDeposit(type) {
        this.pingzheng = true
        this.showpingzheng = false
        if (type == 1) {
          chargeRent(this.orderNo)
            .then((res) => {
              this.costDetail();
              this.$message.success(res.msg);
            })
        } else {
          chargeDeposit(this.orderNo)
            .then((res) => {
              this.costDetail();
              this.$message.success(res.msg);
            })
        }
      },
      getSummaries(param) {
        const {
          columns,
          data
        } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index === 1) {
            const values = data.map(item => Number(item[column.property]));
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += ' 元';
          }
        });

        return sums;
      },
      confirmReceipt(row) {
        this.btnLoadIng = true;
        confirmReceipt(row.id).then(response => {
          this.costDetail();
          this.btnLoadIng = false
        }).catch(err => {
          this.btnLoadIng = false
        });
      },

      confirmReceiptAll() {
        this.btnLoadIng = true;
        confirmReceiptAll(this.orderNo).then(response => {
          this.costDetail();
          this.btnLoadIng = false
        }).catch(err => {
          this.btnLoadIng = false
        });
      },
      objectSpanMethod({
        row,
        column,
        rowIndex,
        columnIndex
      }) {
        let rowspan = this.detail.chargingItems.length - 2;
        if (columnIndex === 0 || columnIndex > 5) {
          if (rowIndex === 0 && rowIndex < rowspan) {
            return {
              rowspan: rowspan,
              colspan: 1
            };
          } else if (rowIndex > 0 && rowIndex < rowspan) {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }

        if (columnIndex > 5 && rowIndex >= rowspan) {
          if (rowIndex === rowspan) {
            return {
              rowspan: 2,
              colspan: 1
            };
          } else {
            return {
              rowspan: 0,
              colspan: 0
            };
          }
        }
        if (rowIndex >= rowspan) {
          if (columnIndex === 0) {
            return [1, 2];
          } else if (columnIndex === 1) {
            return [0, 0];
          } else if (columnIndex === 5) {
            return [1, 4];
          } else if (columnIndex < 5) {
            return [0, 0];
          }
        }
      }

    },

  }
</script>

<style>
</style>
